﻿using GDGeek;
using MrPP.ProgramPlatform;
using MrPP.SerriedCabinet;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

namespace MrPP.SocketSystem { 
    public class SockeLinker : GDGeek.Singleton<SockeLinker>
    {
        [SerializeField]
        private string _ip = "127.0.0.1";
        [SerializeField]
        private int _port = 8484;
        [SerializeField]
        private UnityEvent _onConnect;
        [SerializeField]
        private UnityEvent _onFailed;
        private bool connect_ = false;
        private bool failed_ = false;
       void Start() {
            SocketManager.Instance.connect(_ip, _port, delegate {
                connect_ = true;
            }, delegate {
                failed_ = true;
              
            });
            
        }


        public DataTask<bool> link(string ip, int port) {
            DataTask<bool> task = new DataTask<bool>();
            TaskManager.PushFront(task, delegate {
                SocketManager.Instance.connect(ip, port, delegate {
                    task.setData(true);
                }, delegate {
                    task.setData(false);
                });
            });
            return task;
        }

       
        void Update()
        {
            if (connect_)
            {
                connect_ = false;
                _onConnect?.Invoke();
                TaskWait tw = new TaskWait(3);
                TaskManager.PushBack(tw, delegate ()
                {
                   
                    SerriedCabinetManager.Instance.requestAreaColumnConfiguration();
                    SerriedCabinetManager.Instance.requestTheInitialStatus();
                });
                TaskManager.Run(tw);
            }
            if (failed_)
            {
                failed_ = false;
                _onFailed.Invoke();
            }
        }

    }
}